import 'package:flutter/cupertino.dart';

import 'Constant.dart';
import 'convert/convert.dart';
import 'widget/divider_widget.dart';
import 'widget/doc_widget.dart';
import 'widget/text_widget.dart';
import 'widget/image_group_widget.dart';
import 'widget/image_widget.dart';
import 'widget/audio_widget.dart';
import 'widget/video_widget.dart';

class PreviewPage extends StatefulWidget {
  final String data;
  final ScrollPhysics? physics;
  final bool shrinkWrap;
  final EdgeInsets? padding;

  const PreviewPage(this.data,
      {this.physics, this.shrinkWrap = false, this.padding});

  @override
  _PreviewPageState createState() => _PreviewPageState();
}

class _PreviewPageState extends State<PreviewPage> {
  late List _dataList;

  @override
  void initState() {
    _dataList = Convert.string2EntityList(widget.data);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
          physics: widget.physics ?? const BouncingScrollPhysics(),
          padding: widget.padding,
          shrinkWrap: widget.shrinkWrap,
          itemCount: _dataList.length,
          itemBuilder: (context, index) {
            return _addItem(_dataList[index]);
          }),
    );
  }

  Widget _addItem(dynamic data) {
    Widget widget;
    switch (data.name) {
      case WidgetType.Text:
        widget = TextPreview(data);
        break;
      case WidgetType.Divider:
        widget = DividerPreview(data);
        break;
      case WidgetType.Image:
        widget = ImagePreview(data);
        break;
      case WidgetType.Video:
        widget = VideoPreview(data);
        break;
      case WidgetType.Audio:
        widget = AudioPreview(data);
        break;
      case WidgetType.Doc:
        widget = DocPreview(data);
        break;
      case WidgetType.ImageGroup:
        widget = ImageGroupPreview(data);
        break;
      default:
        widget = Text("TODO");
    }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        widget,
        Padding(
          padding: const EdgeInsets.all(4),
        ),
      ],
    );
  }
}
